import http from '../utils/http';
import { CREATE_ITEMBANK,SET_ITEMBANK_LIST,SET_ITEMBANK_RELATED_QUESTIONS,REMOVE_ITEMBANK} from './types';

export function setItembankCreateResult(result) {
  return {
    type: CREATE_ITEMBANK,
    result
  };
}

export function setItembankList(itembanks) {
  return {
    type: SET_ITEMBANK_LIST,
    itembanks
  };
}

export function setItembankRelatedQuestions(questions) {
  return {
    type: SET_ITEMBANK_RELATED_QUESTIONS,
    questions
  }
}


export function removeItembank(itembank){
  return {
    type:REMOVE_ITEMBANK,
    itembank
  }
}

export function saveItembank(data) {  
  return dispatch => {
    return http.post('/api/itembanks', data).then(res => {
          return res ; 
        });
  }
}

export function fetchItembank(id){
    return dispatch => {
      return http.get('/api/itembanks/'+id).then(res => {
            return res ; 
          });
     }
}

export function fetchItembanks(){
    return dispatch => {
      return http.get('/api/itembanks').then(res => {
            if (res.data.code !== 10200 ){
              dispatch(setItembankList([]));
            }else {
              dispatch(setItembankList(res.data.body.itembanks));
            }
          });
     }
}

export function deleteItembank(id){
  return dispatch => {
    return http.post('/api/itembanks/'+id+'/remove', {id}).then(res => {
          if (res.data.code === 10200 ){
            dispatch(removeItembank(id));
          }
        });
  }
}

export function enableItembank(id){
  return dispatch => {
    return http.post('/api/itembanks/'+id+'/enable', {id}).then(res => {
          if (res.data.code !== 10200 ){
            // dispatch(setItembankCreateResult(false));
          }else {
            // dispatch(setItembankCreateResult(true));
          }
        });
  }
}

export function disableItembank(id){
  return dispatch => {
    return http.post('/api/itembanks/'+id+'/disable', {id}).then(res => {
          
        });
  }
}

export function relateWithQuestions(id,questions){
    return dispatch => {
      return http.post('/api/itembanks/'+id+'/questions/add', {questions}).then(res => {
        return res ; 
      });
  }
}

export function removeRelatedQuestions(id,questions) {
  return dispatch => {
    return http.post('/api/itembanks/'+id+'/questions/remove', {questions}).then(res => {
          if (res.data.code !== 10200 ){
          }else {
          }
        });
  }
}

export function fetchRelatedQuestions(id){
    return dispatch => {
      return http.get('/api/itembanks/'+id+'/questions').then(res => {
          if (res.data.code === 10200 ){
            dispatch(setItembankRelatedQuestions(res.data.body.questions));
          }
          return res ; 
        });
     }
}